//
//  CinemaViewController.m
//  DaHaiDouBan
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 Wang Dahai. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaCell.h"
#import "AppTool.h"
#import "Cinema.h"
#import "DataBaseSingleton.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *cinemaArray;
@property (nonatomic, assign) BOOL isUpLoad;

@end

@implementation CinemaViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setAutoresizesSubviews:YES];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"影院";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    [self.myTableView registerClass:[CinemaCell class] forCellReuseIdentifier:@"cell"];
//        //沙盒取数据
//    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    
//    NSString *cinemaArrayPath = [document stringByAppendingPathComponent:@"cinema.xml"];
//    
//    self.cinemaArray = [NSKeyedUnarchiver unarchiveObjectWithFile:cinemaArrayPath];
    self.isUpLoad = NO;
    //初始化属性数组
    self.cinemaArray = [NSMutableArray array];
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    
    //先查询数据库,如果数据库没有缓存数据,则从AFN请求数据,然后将请求的数据放入到数据库的表格中
    [[DataBaseSingleton shareDataBase] openDB];
    [[DataBaseSingleton shareDataBase] createTable];
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectAllCinema];
    self.cinemaArray = [NSMutableArray arrayWithArray:array];
    
        NSString *url_string = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
        [AppTool getDataWithAFNFromURL:url_string andBlock:^(id result) {
            
            NSMutableDictionary *dic = result;
            NSString *reason = [dic objectForKey:@"reason"];
            if ([reason isEqualToString:@"success"]) {
                NSMutableDictionary *bigDic = [dic objectForKey:@"result"];
                NSMutableArray *array = [bigDic objectForKey:@"data"];
                for (NSMutableDictionary *smallDic in array) {
                    Cinema *cinema = [[Cinema alloc] init];
                    [cinema setValuesForKeysWithDictionary:smallDic];
                    [self.cinemaArray addObject:cinema];
                    //插入表
                    [[DataBaseSingleton shareDataBase] insertTabel:cinema];
                    
                    [cinema release];
                }
            }else
            {
                NSLog(@"请求失败,是的");
               
                
            }
            [self.myTableView reloadData];
            //归档写入本地
            //  [NSKeyedArchiver archiveRootObject:self.cinemaArray toFile:cinemaArrayPath];
        }];

}

-(void)addHeaderRefresh
{
    [self.myTableView addHeaderWithCallback:^{
        NSLog(@"下拉");
        self.isUpLoad = NO;
        
        [self getAFNetWorkingData];
        
        
    }];
    
    [self.myTableView headerBeginRefreshing];
}
-(void)addFooterRefresh
{
    
    [self.myTableView addFooterWithCallback:^{
        NSLog(@"上拉");
        self.isUpLoad = YES;
        
        [self getAFNetWorkingData];
        
    }];
    
    [self.myTableView footerBeginRefreshing];
    
}

-(void)getAFNetWorkingData
{
    NSString *url_string = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
    [AppTool getDataWithAFNFromURL:url_string andBlock:^(id result) {
        
        if (self.isUpLoad == NO) {
            
            [self.cinemaArray removeAllObjects];
            [[DataBaseSingleton shareDataBase] dropTable];
            [[DataBaseSingleton shareDataBase] createTable];
            NSLog(@"清除数组");
        }else
        {
            
            NSLog(@"不用清空数组");
            
        }
        
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        if ([reason isEqualToString:@"success"]) {
            NSMutableDictionary *bigDic = [dic objectForKey:@"result"];
            NSMutableArray *array = [bigDic objectForKey:@"data"];
            for (NSMutableDictionary *smallDic in array) {
                Cinema *cinema = [[Cinema alloc] init];
                [cinema setValuesForKeysWithDictionary:smallDic];
                [self.cinemaArray addObject:cinema];
                //插入表
                [[DataBaseSingleton shareDataBase] insertTabel:cinema];
                
                [cinema release];
            }
        }else
        {
            NSLog(@"请求失败,走了");
            [self.myTableView footerEndRefreshing];
            [self.myTableView headerEndRefreshing];
            
        }
        [self.myTableView reloadData];
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        //归档写入本地
        //  [NSKeyedArchiver archiveRootObject:self.cinemaArray toFile:cinemaArrayPath];
    }];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cinemaArray.count;
   // return [[[DataBaseSingleton shareDataBase] selectAllCinema] count];
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdetifier = @"cell";
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier];
    cell.cinema = [self.cinemaArray objectAtIndex:indexPath.row];
    //Cinema *cinema = [[[DataBaseSingleton shareDataBase] selectAllCinema] objectAtIndex:indexPath.section];
    //cell.cinema = cinema;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 140;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
